package cn.buk.tms.eotms.controller;

import cn.buk.common.dto.CommonDto;
import cn.buk.tms.eotms.dto.SettlementReportDto;
import cn.buk.tms.eotms.entity.BillInfo;
import cn.buk.tms.eotms.response.SettlementSheetResponseDto;
import cn.buk.tms.eotms.sc.SearchBillInfoCriteria;
import cn.buk.tms.eotms.sc.SearchSettlementCriteria;
import cn.buk.tms.eotms.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 账单
 * @author yfdai
 */
@RestController
public class BillController extends BaseController {

  @Autowired
  private BillService billService;

  /**
   * 查找差旅客户的订单
   *
   * @param request
   * @param sc 查询条件
   * @param billType 账单类型
   * @param bePaid 收款状态
   * @param paymentStatus 支出状态
   * @param payType 账单的付款方式
   * @param reviewed 审核状态
   * @return
   */
  @RequestMapping("/my/bills")
  public CommonDto<BillInfo> searchMyBillInfo(HttpServletRequest request,
      @ModelAttribute("sc") SearchBillInfoCriteria sc,
      @RequestParam("billType") String billType,
      @RequestParam(value = "bePaid", required = false) String bePaid,
      @RequestParam(value = "paymentStatus", defaultValue = "-1") int paymentStatus,
      @RequestParam(value = "payType", defaultValue = "0") int payType,
      @RequestParam(value = "reviewed", defaultValue = "-1") int reviewed) {

    sc.setBillType(billType);
    sc.setBePaid(bePaid);
    sc.setPayType(payType);
    sc.setReviewed(reviewed);
    sc.setPaymentStatus(paymentStatus);

    List<BillInfo> dataList = billService.searchBills(getUserId(request), sc);

    CommonDto<BillInfo> commonDto = new CommonDto<>();
    commonDto.setDataList(dataList);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 查找账单详情
   */
  @RequestMapping("/my/bill/{id}")
  public BillInfo searchBillInfoDetail(HttpServletRequest request,
      @PathVariable(value = "id") int id) {
    return billService.searchBillInfoDetail(getUserId(request), id);
  }

  /**
   * 查找结算单
   */
  @RequestMapping({"/settlements", "/my/settlements"})
  public CommonDto<SettlementSheetResponseDto> searchSettlements(
          HttpServletRequest request,
          @RequestParam(value = "settlementNo", defaultValue = "") final String settlementNo,
          @RequestParam(value = "payStatus", defaultValue = "-1") final int payStatus,
          @RequestParam(value = "pageNo", defaultValue = "1") final int pageNo,
          @RequestParam(value = "pageSize", defaultValue = "25") final int pageSize
  ) {
    SearchSettlementCriteria sc = new SearchSettlementCriteria();
    sc.setSettlementNo(settlementNo);
    sc.setStatus(payStatus);
    sc.setPageNo(pageNo);
    sc.setPageSize(pageSize);

    List<SettlementSheetResponseDto> dataList = billService.searchSettlements(getUserId(request), sc);

    CommonDto<SettlementSheetResponseDto> commonDto = new CommonDto<>();
    commonDto.setDataList(dataList);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 结算单报表
   */
  @GetMapping({"/settlement/{id}", "/my/settlement/{id}/report"})
  public SettlementReportDto searchSettlementReport(HttpServletRequest request,
                                                    @PathVariable("id") int id) {
    return billService.searchSettlementReport(getUserId(request), id);
  }
}
